/*
 * UI2.java
 *
 * Created on August 20, 2008, 1:19 PM
 */

package UIDebugger;

import Server.Server;
import data.levels.Level1;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Vector;
import plar.Element;
import plar.Elements.Player;

/**
 *
 * @author  spok
 */
public class UI2 extends javax.swing.JFrame {

    /** Creates new form UI2 */
    Server s;
    private Player currentPlayer;
    public UI2() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnClock = new javax.swing.JButton();
        btnInitialize = new javax.swing.JButton();
        gameFrame = new UIDebugger.GamePanel();
        btnRefresh = new javax.swing.JButton();
        btnClockAndRefresh = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        btnClock.setText("Clock()");
        btnClock.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnClockMouseClicked(evt);
            }
        });

        btnInitialize.setText("Initialize");
        btnInitialize.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnInitializeMouseClicked(evt);
            }
        });
        btnInitialize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInitializeActionPerformed(evt);
            }
        });

        gameFrame.setBackground(new java.awt.Color(191, 239, 239));

        javax.swing.GroupLayout gameFrameLayout = new javax.swing.GroupLayout(gameFrame);
        gameFrame.setLayout(gameFrameLayout);
        gameFrameLayout.setHorizontalGroup(
            gameFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 509, Short.MAX_VALUE)
        );
        gameFrameLayout.setVerticalGroup(
            gameFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 306, Short.MAX_VALUE)
        );

        btnRefresh.setText("Refresh()");
        btnRefresh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRefreshMouseClicked(evt);
            }
        });

        btnClockAndRefresh.setText("Clock&Refresh()");
        btnClockAndRefresh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnClockAndRefreshMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnInitialize)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClock)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRefresh)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClockAndRefresh)
                        .addGap(2, 2, 2))
                    .addComponent(gameFrame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInitialize)
                    .addComponent(btnClock)
                    .addComponent(btnRefresh)
                    .addComponent(btnClockAndRefresh))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gameFrame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnInitializeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInitializeActionPerformed
 s = new Server(new Level1());
 currentPlayer = s.addPlayer("pippo");
 currentPlayer.setPosition(new Point(300,20));
}//GEN-LAST:event_btnInitializeActionPerformed

private void btnInitializeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnInitializeMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_btnInitializeMouseClicked

private void btnClockMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnClockMouseClicked
    s.level.clock();

}//GEN-LAST:event_btnClockMouseClicked

private void btnRefreshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRefreshMouseClicked
  viewOutput(getOutput());
}//GEN-LAST:event_btnRefreshMouseClicked

private void btnClockAndRefreshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnClockAndRefreshMouseClicked
 s.level.clock();
 viewOutput(getOutput());
}//GEN-LAST:event_btnClockAndRefreshMouseClicked

private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
 s = new Server(new Level1());
 currentPlayer = s.addPlayer("pippo");
 currentPlayer.setPosition(new Point(300,20));
}//GEN-LAST:event_formComponentShown
public Vector <ScreenElement>  getOutput()
{
    Vector <Element> elems =  s.getScreen(currentPlayer);
    
    Vector <ScreenElement> returnMe = new Vector <ScreenElement> ();
    for ( Element e : elems )
    {
        String filename = e.getCurrentSprite().filename;
        Rectangle p  = e.getPosition();
        System.out.println("getOutput(): "+filename+" ["+p.x+","+p.y+"]");
        returnMe.add(new ScreenElement(e.getCurrentSprite(),new Rectangle(p)));
    }
    return returnMe;
}

public void viewOutput(Vector <ScreenElement> elems)
{ 
     gameFrame.viewOutput(elems);
}
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UI2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClock;
    private javax.swing.JButton btnClockAndRefresh;
    private javax.swing.JButton btnInitialize;
    private javax.swing.JButton btnRefresh;
    private UIDebugger.GamePanel gameFrame;
    // End of variables declaration//GEN-END:variables

}
